package com.peki.Service.Impl;


import com.peki.Dao.ProductsDao;
import com.peki.Model.Products;
import com.peki.Service.ProductService;
import com.peki.Util.RedisUtil;
import com.peki.Util.bean.CommonQueryBean;
import com.peki.base.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.mail.MailParseException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("productServiceImpl")
@Slf4j
public class ProductServiceImpl implements ProductService {


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ProductsDao productsDao;

    @Override
    public BaseResponse add(http req) throws Exception {
        String theName = req.getName();
        String theBrand = req.getBrand();
        String theCost = req.getCost();
        String theEvaluate = req.getEvaluate();
        
        Products products = new Products();
        products.setBrand(theBrand);
        products.setName(theName);
        products.setCost(theCost);
        products.setEvaluate(theEvaluate);

        //首先判断该商品的各个标签是否重复
        if (redisUtil.hasKey(theCost)&&redisUtil.hasKey(theBrand)&&redisUtil.hasKey(theEvaluate))
        {
           Products products1 = productsDao.select(theName,theCost,theBrand,theEvaluate);
           if(products1.equals(products))
           {
               return BaseResponse.error(ErrorMessage.REPEAT_ERROR);
           }
           else
           {
               return getBaseResponse(products, theBrand, theName, theCost, theEvaluate);
           }
        }
        else
        {
            return getBaseResponse(products, theBrand, theName, theCost, theEvaluate);
        }

    }

    private BaseResponse getBaseResponse(Products products, String theBrand, String theName, String theCost, String theEvaluate) {
        productsDao.insert(products);
        log.info("===[已放入数据库！]===");

        redisUtil.set(theBrand, theName);
        redisUtil.set(theCost, theName);
        redisUtil.set(theEvaluate, theName);
        log.info("===[已放入缓存！]===");
        return BaseResponse.error(ErrorMessage.Finish);
    }


    @Override
    public BaseResponse update(http req) throws Exception {
        String theName = req.getName();
        String theBrand = req.getBrand();
        String theCost = req.getCost();
        String theEvaluate = req.getEvaluate();
        if (theBrand != null) {
            if (redisUtil.hHasKey(theBrand, theName)) {
                return BaseResponse.error(ErrorMessage.therepeat);
            } else {
                redisUtil.set(theBrand, theName);

            }

        }
        if (theCost != null) {
            if (redisUtil.hHasKey(theCost, theName)) {
                return BaseResponse.error(ErrorMessage.therepeat);
            } else {
                redisUtil.set(theCost, theName);
            }
        }
        if (theEvaluate != null) {
            if (redisUtil.hHasKey(theEvaluate, theName)) {
                return BaseResponse.error(ErrorMessage.therepeat);
            } else {
                redisUtil.set(theEvaluate, theName);
            }
        }
        return BaseResponse.error(ErrorMessage.Finish);
    }


    @Override
    public BaseResponse delete(http req) throws Exception {

        String theName = req.getName();
        String theBrand = req.getBrand();
        String theCost = req.getCost();
        String theEvaluate = req.getEvaluate();

        if (theBrand != null) {
            if (redisUtil.hHasKey(theBrand, theName)) {
                redisUtil.del(theBrand, theName);
            } else {
                return BaseResponse.error(ErrorMessage.NOTFOUND);
            }

        }

        if (theCost != null) {
            if (redisUtil.hHasKey(theCost, theName))
            {
                redisUtil.del(theCost, theName);
            }
            else
            {
                return BaseResponse.error(ErrorMessage.NOTFOUND);
            }

        }

        if (theEvaluate != null) {
            if (redisUtil.hHasKey(theEvaluate, theName)) {
                redisUtil.del(theEvaluate, theName);
            } else
            {
                return BaseResponse.error(ErrorMessage.NOTFOUND);
            }

        }
        return BaseResponse.error(ErrorMessage.Finish);
    }


    @Override
    public PageResp <Products> list(SearchHttp req) throws Exception {

        String theBrand = req.getBrand();
        String theCost = req.getCost();
        String theEvaluate = req.getEvaluate();

        Products p = new Products();

        p.setEvaluate(theEvaluate);
        p.setBrand(theBrand);
        p.setCost(theCost);

        //物理分页
        List<Products> list = productsDao.list4Page(p);

        PageResp pageResp = new PageResp();
        pageResp.setList(list);

        return pageResp;
    }


}
